package com.bocai.kelalacar.base;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.R;
import com.bocai.kelalacar.util.AppManager;
import com.bocai.kelalacar.util.ToastUitl;
import com.zhy.autolayout.AutoLayoutActivity;

import butterknife.ButterKnife;


/**
 * 基类
 */

/***************
 * 使用例子
 *********************/
public abstract class BaseActivity extends AutoLayoutActivity {
    public Context mContext;
    private ProgressDialog mDialog;
    public Bundle mBundle;
    public Toolbar mToolbar;
    public TextView mTitleTv;
    public ImageView ImgRight;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doBeforeSetcontentView();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
//        EventBus.getDefault().register(this);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        mBundle = savedInstanceState;
        mContext = this;
        this.initPresenter();
        this.initView();
    }


    public void setup(String title) {
        TextView textView = (TextView) findViewById(R.id.tv_title);
        textView.setText(title);
    }

    public void setup(int title, int resId, View.OnClickListener onClickListener) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView textView = (TextView) findViewById(R.id.tv_title);
        textView.setText(title);
//        activity.setSupportActionBar(user_toolbar);
        toolbar.setNavigationIcon(resId);
        toolbar.setNavigationOnClickListener(onClickListener);
    }

    public void setup(String title, int resId, View.OnClickListener onClickListener) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView textView = (TextView) findViewById(R.id.tv_title);
        textView.setText(title);
//        activity.setSupportActionBar(user_toolbar);
        toolbar.setNavigationIcon(resId);
        toolbar.setNavigationOnClickListener(onClickListener);
    }
    protected void initToolbar(String title) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mTitleTv = (TextView) findViewById(R.id.tv_title);
        mTitleTv.setText(title);
    }
    protected void initBack(ImageView img, LinearLayout ll) {
        img.setImageResource(R.mipmap.back);
        img.setVisibility(View.VISIBLE);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    /**
     * 设置layout前配置
     */
    private void doBeforeSetcontentView() {
//        //设置昼夜主题
//        initTheme();
        // 把actvity放到application栈中管理
        AppManager.getAppManager().addActivity(this);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.setStatusBarColor(Color.TRANSPARENT);
//        }
    }

    /*********************
     * 子类实现
     *****************************/
    //获取布局文件
    public abstract int getLayoutId();

    //简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
    public abstract void initPresenter();

    //初始化view
    public abstract void initView();

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 开启浮动加载进度条
     */
    public void startProgressDialog() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(this);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setMessage("加载中...");
        }
        if (!mDialog.isShowing())
            mDialog.show();
    }

    /**
     * 开启浮动加载进度条
     *
     * @param msg
     */
    public void startProgressDialog(String msg) {

        if (msg.equals("")){
            if (mDialog == null) {
                mDialog = new ProgressDialog(this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中...");
            }else {
                mDialog = new ProgressDialog(this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中...");
            }

        }else {
            if (mDialog == null) {
                mDialog = new ProgressDialog(this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage(msg);
            } else {
                mDialog = new ProgressDialog(this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage(msg);
            }
        }
        if (!mDialog.isShowing())
            mDialog.show();

    }

    /**
     * 停止浮动加载进度条
     */
    public void stopProgressDialog() {
        if (mDialog != null)
            if (mDialog.isShowing())
                mDialog.dismiss();
    }

    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUitl.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUitl.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUitl.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUitl.showLong(text);
    }

    //    /**
//     * 带图片的toast
//     * @param text
//     * @param res
//     */
//    public void showToastWithImg(String text, int res) {
//        ToastUitl.showToastWithImg(text,res);
//    }
//    /**
//     * 网络访问错误提醒
//     */
//    public void showNetErrorTip() {
//        ToastUitl.showToastWithImg(getText(R.string.net_error).toString(),R.mipmap.ic_launcher);
//    }
//    public void showNetErrorTip(String error) {
//        ToastUitl.showToastWithImg(error,R.mipmap.ic_launcher);
//    }
    @Override
    protected void onResume() {
        super.onResume();
        //debug版本不统计crash
    }

    @Override
    protected void onPause() {
        super.onPause();
        //debug版本不统计crash
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);

    }
}
